package com.hdl.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类名 ZookeeperConfiguration
 * 描述 zookeeper配置
 *
 * @author hedonglin
 * @version 1.0
 * @date 2020/6/17 15:55
 */
@Configuration
public class ZookeeperConfiguration {
    @Value("${zk.connectString:127.0.0.1:2181}")
    private String connectString;

    @Value("${zk.sessionTimeoutMs:5000}")
    private int sessionTimeoutMs;

    @Value("${zk.connectionTimeoutMs:5000}")
    private int connectionTimeoutMs;

    @Value("${zk.namespace:hdlrpc}")
    private String namespace;

    @Bean
    public CuratorFramework curatorFramework(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework zkClient = CuratorFrameworkFactory.builder()
                //server地址
                .connectString(connectString)
                // 会话超时时间
                .sessionTimeoutMs(sessionTimeoutMs)
                // 连接超时时间
                .connectionTimeoutMs(connectionTimeoutMs)
                // 重试策略
                .retryPolicy(retryPolicy)
                // 独立命名空间/base
                .namespace(namespace)
                .build();
        zkClient.start();
        return zkClient;
    }
}
